// pages/orderList/orderList.js
var httpClient = require('../../utils/httpClient.js');
var utils = require('../../utils/util.js');
var dateUtil = require('../../utils/dateUtil.js');
Page({
  data: {
    settlement: {},
    orders: [],
    currentDay: {
      year: "2018",
      month: "01",
      day: "01"
    },
    pageNo: 0,
    showBottom: false,
    currentDayFormat: "",
    dayPicker: {
      startDay: "2016-10-20",
      endDay: dateUtil.format(new Date(), "yyyy-MM-dd"),
      value: dateUtil.format(new Date(), "yyyy-MM-dd")
    }
  },
  dayPickerChange: function (e) {
    this.refresh(new Date(e.detail.value));
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.refresh(new Date());
  },
  refresh: function (date) {
    this.formatDate(date);
    this.setStartTime();
    this.getSettlement();
  },
  setStartTime: function (callback) {
    var page = this;
    httpClient.get("/shop/get/" + wx.getStorageSync("shopId"), function (result) {
      var dayPicker = page.data.dayPicker;
      dayPicker.startDay = dateUtil.format(new Date(result.createTime), 'yyyy-MM-dd')
      page.setData({
        dayPicker: dayPicker
      });
    });
  },
  comboDay: function (lang, day) {
    if (lang == "en") {
      return day.year + "-" + day.month + "-" + day.day;
    } else {
      return day.year + "年" + day.month + "月" + day.day + "日";
    }
  },
  formatDate: function (date) {
    var currentDay = {
      year: date.getFullYear(),
      month: date.getMonth() + 1,
      day: date.getDate()
    }
    if (currentDay.month < 10) {
      currentDay.month = "0" + currentDay.month
    }
    if (currentDay.day < 10) {
      currentDay.day = "0" + currentDay.day
    }
    this.setData({
      currentDay: currentDay,
      currentDayFormat: this.comboDay("zh", currentDay)
    });
  },
  getSettlement: function (callback) {
    var that = this;
    httpClient.get("/settlement/getDay/" + wx.getStorageSync("shopId") + "/" + this.comboDay("en", this.data.currentDay), function (result) {
      result.salesFormat = utils.outputmoney(result.sales);
      that.setData({
        settlement: result,
        pageNo: 0,
        orders: [],
        showBottom: false
      });
      that.queryOrder(that.data.pageNo, callback);
    });
  },
  queryOrder: function (pageNo, callback) {
    var that = this;
    var req = {
      page: {
        pageSize: 10,
        pageNo: pageNo
      },
      shopId: wx.getStorageSync("shopId"),
      day: this.comboDay("en", this.data.currentDay)
    };
    httpClient.post("/order/queryOrderGroup", req, function (result) {
      for (var i in result.orders) {
        var order = result.orders[i];
        order.payMethodFormat = utils.payMethodMap[order.payMethod];
        order.totalPriceFormat = utils.outputmoney(order.totalPrice);
        order.settlementTimeFormat = utils.formatTime(new Date(order.settlementTime));
      }
      var orders = that.data.orders;
      orders = orders.concat(result.orders);
      that.setData({
        orders: orders
      });
      if (callback) {
        callback();
      }
      if (orders.length > 0 && result.orders.length == 0) {
        that.setData({
          showBottom: true
        })
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getSettlement(function () {
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.showBottom) {
      return;
    }
    var pageNo = this.data.pageNo;
    pageNo++;
    this.setData({
      pageNo: pageNo
    });
    this.queryOrder(pageNo)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})